<?php
/**
 */
class PromotionApp extends BackendApp
{
    var $_promotion_mod;

    function __construct()
    {
        $this->PromotionApp();
    }
    function PromotionApp()
    {
        parent::BackendApp();

        $this->_promotion_mod =& m('promotion');
    }

    /* 商品列表 */
    function index()
    {
        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'goods_name',
                'equal' => 'like',
            ),
            array(
                'field' => 'store_name',
                'equal' => 'like',
            ),
            array(
                'field' => 'pro_name',
                'equal' => 'like',
            ),
        ));

        
        //更新排序
        if (isset($_GET['sort']) && isset($_GET['order']))
        {
            $sort  = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order,array('asc','desc')))
            {
             $sort  = 'g.goods_id';
             $order = 'desc';
            }
        }
        else
        {
            $sort  = 'g.goods_id';
            $order = 'desc';
        }

        $page = $this->_get_page();
        $promotion_list = $this->_promotion_mod->find(array(
            'join' => 'belong_goods,belongs_to_store',
            'conditions' => "1 = 1" . $conditions,
            'count' => true,
            'order' => "$sort $order",
            'limit' => $page['limit'],
        ));
        $this->assign('promotion_list', $promotion_list);

        $page['item_count'] = $this->_promotion_mod->getCount();
        $this->_format_page($page);
        $this->assign('page_info', $page);

        $this->import_resource(array('script' => 'mlselection.js,inline_edit.js'));
        $this->display('promotion.index.html');
    }



    //异步修改数据
   function ajax_col()
   {
       $id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
       $column = empty($_GET['column']) ? '' : trim($_GET['column']);
       $value  = isset($_GET['value']) ? trim($_GET['value']) : '';
       $data   = array();

       if (in_array($column ,array('pro_recommended','pro_name')))
       {
           $data[$column] = $value;
           $this->_promotion_mod->edit($id, $data);
           if(!$this->_promotion_mod->has_error())
           {
               echo ecm_json_encode(true);
           }
       }
       else
       {
           return ;
       }
       return ;
   }

    /* 删除商品 */
    function drop() {
        $id = isset($_GET['id']) ? trim($_GET['id']) : '';
        if (!$id) {
            $this->show_warning('Hacking Attempt');
            return;
        }
        $ids = explode(',', $id);

        if (!$this->_promotion_mod->drop($ids)) {
            $this->show_warning($this->_promotion_mod->get_error());
            return;
        }else{
            $this->admin_log(Lang::get('del_promotion'),1);
        }
        $ret_page = isset($_GET['ret_page']) ? intval($_GET['ret_page']) : 1;
        $this->show_message('drop_ok', 'back_list', 'index.php?app=promotion&page=' . $ret_page);
    }
}

?>
